<?php
/**
 * Plugin Blogr: Embedding Blogr image
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Juergen A. Lamers <jaloma.ac@googlemail.com>
 * @version    0.1.0
 */

if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');

/**
 * All DokuWiki plugins to extend the parser/rendering mechanism
 * need to inherit from this class
 */
class syntax_plugin_blogr extends DokuWiki_Syntax_Plugin {

    /**
     * return some info
     */
    function getInfo(){
        return array(
            'author' => 'Juergen A. Lamers',
            'email'  => 'jaloma.ac@googlemail.com',
            'date'   => '2007-01-06',
            'name'   => 'Blogr Plugin',
            'desc'   => 'Embedding Blogr image',
            'url'    => 'http://jaloma.ac.googlepages.com/plugin:blogr',
        );
    }

    /**
     * What kind of syntax are we?
     */
    function getType(){
        return 'substition';
    }

    /**
     * What kind of syntax do we allow (optional)
     */
//    function getAllowedTypes() {
//        return array();
//    }

    /**
     * What about paragraphs? (optional)
     */
//    function getPType(){
//        return 'normal';
//    }

    /**
     * Where to sort in?
     */
    function getSort(){
        return 299;
    }


    /**
     * Connect pattern to lexer
     */
    function connectTo($mode) {
      $this->Lexer->addSpecialPattern('\{\{blogr>.*?\}\}',$mode,'plugin_blogr');
    }

    /**
     * Handle the match
     */
    function handle($match, $state, $pos, &$handler){
        switch ($state) {
          case DOKU_LEXER_ENTER :
            break;
          case DOKU_LEXER_MATCHED :
            break;
          case DOKU_LEXER_UNMATCHED :
            break;
          case DOKU_LEXER_EXIT :
            break;
          case DOKU_LEXER_SPECIAL :
            return $match;
            break;
        }
        return array();
    }

    /**
     * Create output
     */
    function render($mode, &$renderer, $data) {
      if($mode == 'xhtml'){
	global $conf;
	//123456789
	//flickr
	//blogr
	// strip blogr> from start
	$data = substr($data,8,-2);
	@list($key, $staticurl, $photoid, $alt) = explode('|',$data,4);

	// parse the media az an external media
	$p = Doku_Handler_Parse_Media("{{".$staticurl."}}");
	if ($p['title'] == '') {
	  $p['title'] = $alt;
	}
	// render the media as a blogr external media
	$link = array();
	$link['class']  = 'media';
	$link['style']  = '';
	$link['pre']    = '';
	$link['suf']    = '';
	$link['more']   = '';
	$link['target'] = $conf['target']['media'];
	if (!isset($key) || $key == '') {
	  $accounturl = $this->getConf('accounturl');
	} else {
	  $accounturl = $key;
	}
	$link['title']  = $p['title'];
	$link['url']    = "http://".$accounturl.".blogr.de/photos/".$photoid."/";

	/* */
	$link['name']   = $renderer->_media ($p['src'], $p['title'], $p['align'], $p['width'], $p['height'], $p['cache']);
			
	list($ext,$mime) = mimetype($p['src']);
	if(substr($mime,0,5) == 'image'){
	  // link only jpeg images
	  // if ($ext != 'jpg' && $ext != 'jpeg') $noLink = TRUE;
	}elseif($mime == 'application/x-shockwave-flash'){
	  // don't link flash movies
	  $noLink = TRUE;
	}else{
	  // add file icons
	  $link['class'] .= ' mediafile mf_'.$ext;
	}

	//output formatted
	$renderer->doc .= $renderer->_formatLink($link);
	/* */
	return true;
      }
      return false;
    }
}
